package com.daxia.bumall.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.daxia.bumall.common.AuditStatus;
import com.daxia.bumall.common.Logs;
import com.daxia.bumall.daobase.GenericDAOHibernate;
import com.daxia.bumall.dto.CompanyDTO;
import com.daxia.bumall.model.Company;
import com.daxia.bumall.model.User;
import com.daxia.bumall.support.Page;
import com.daxia.bumall.util.SpringSecurityUtils;

@Repository
public class CompanyDAO extends GenericDAOHibernate<Company> {

    public List<Company> find(CompanyDTO dto, Page page) {
        StringBuilder hql = new StringBuilder();
        List<Object> paras = new ArrayList<Object>();
        hql.append("From Company n where 1 = 1 ");
        if (dto.getId() != null) {
            hql.append(" and n.id = ? ");
            paras.add(dto.getId());
        }
        if (dto.getUser() != null) {
        	if (dto.getUser().getId() != null) {
	            hql.append(" and n.user.id = ? ");
	            paras.add(dto.getUser().getId());
        	} else {
	        	if (dto.getUser().getCity() != null) {
	        		hql.append(" and n.city.id = ? ");
		            paras.add(dto.getUser().getCity().getId());
	        	}
        	}
        }

		if(StringUtils.isNotBlank(dto.getName())){
        	hql.append(" and n.name like ? ");
	         paras.add("%"+dto.getName()+"%");
        }
        if(dto.getProvince()!=null){
        	 hql.append(" and n.province.id = ? ");
	         paras.add(dto.getProvince().getId());
        }
        if(dto.getDistrict()!=null){
        	hql.append(" and n.district.id = ? ");
        	paras.add(dto.getDistrict().getId());
        }

        if (dto.getCity() != null && dto.getCity().getId() != null) {
        	hql.append(" and n.city.id = ? ");
	        paras.add(dto.getCity().getId());
        }
        

        if (StringUtils.isNotBlank(dto.getSearchTerm())) {
        	hql.append(" and (n.user.username like ? or"
        			+ " n.user.nickName like ? "
        			+ " or n.user.realName like ?"
        			+ " or n.user.mobile like ?"
        			//+ " or (n.user.province.id is not null and n.user.province.name like ?) "
        			//+ " or (n.user.city.id is not null and n.user.city.name like ?) "
        			//+ " or (n.user.district.id is not null and n.user.district.name like ?) "
        			+ " or n.mobile like ? "
        			+ " or n.name like ? "
        			+ " or n.registerAddress like ? "
        			+ " or n.officeAddress like ? "
        			+ " or n.auditRemark like ? ) ");
        	//paras.add("%" + dto.getSearchTerm() + "%");
        	//paras.add("%" + dto.getSearchTerm() + "%");
        	//paras.add("%" + dto.getSearchTerm() + "%");
        	paras.add("%" + dto.getSearchTerm() + "%");
        	paras.add("%" + dto.getSearchTerm() + "%");
        	paras.add("%" + dto.getSearchTerm() + "%");
        	paras.add("%" + dto.getSearchTerm() + "%");
        	paras.add("%" + dto.getSearchTerm() + "%");
        	paras.add("%" + dto.getSearchTerm() + "%");
        	paras.add("%" + dto.getSearchTerm() + "%");
        	paras.add("%" + dto.getSearchTerm() + "%");
        	paras.add("%" + dto.getSearchTerm() + "%");
        }
        if (dto.getAuditStatus() != null) {
        	hql.append(" and n.auditStatus = ? ");
        	paras.add(dto.getAuditStatus());
        }
        
        if(dto.getQueryUserId() != null) {
        	hql.append(" and n.user.id = ? ");
        	paras.add(dto.getQueryUserId());
        }
        if (StringUtils.isNotBlank(dto.getName())) {
            hql.append(" and n.name = ? ");
            paras.add(dto.getName());
        }	
        if (dto.isAdmin()) {
        	hql.append(" and n.city.id not in" ).append(dto.getCityIdArr()).append("or n.city.id is null  ");
        }
        hql.append(" order by n.id desc ");
        Logs.COMMON.debug(hql + ", " + paras);
        return super.find(hql.toString(), paras.toArray(), page);
    }
    
    public int updateCompanyStatuS(Long companyId,Integer status) {
    	StringBuffer hql = new StringBuffer();
    	hql.append(" update Company set status = ? where id = ? ");
    	Object[] paras = new Object[]{status,companyId};
    	return super.executeUpdate(hql.toString(), paras);
    }
    
    public Company findCompanyByUserId(Long userId) {
    	StringBuffer hql = new StringBuffer();
    	hql.append(" from Company where user.id = ? ");
    	return (Company) super.findOne(hql.toString(), new Object[]{userId});
    }
    
    public Company findOne(CompanyDTO dto) {
		Page page = new Page();
		page.setPageSize(1);
		page.setPage(1);
		
		List<Company> list = this.find(dto, page);
	    return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }
    
    @Override
    public Long create(Company t) {
    	if (t.getCreator() == null && SpringSecurityUtils.getCurrentUser() != null) {
    		t.setCreator(new User(SpringSecurityUtils.getCurrentUser().getId()));
    	}
    	if (t.getAuditStatus() == null) {
    		t.setAuditStatus(AuditStatus.NotAudit.getValue());
    	}
    	return super.create(t);
    }
}
